import React, { Component } from 'react'

export default class SetState extends Component {
    state = {
        count: 0
    }
    render() {
        return (
            <div>
                <h2>当前和为：{this.state.count}</h2>
                <button onClick={this.add}>+1</button>
            </div>
        )
    }
    add = () => {
        const { count } = this.state
        // 1.setState 是异步的，修改之后不能立马获取
        // this.setState({ count: count+1 })
        // console.log(this.state.count)

        // 2.回调在状态、界面更新之后更新
        // this.setState({ count: count+1 },()=>{
        //     console.log(this.state.count)
        // })
        
        // 3.函数式 setState：新状态依赖原状态时可以使用
        this.setState((state,props)=>{
            console.log(state,props)
            return { count: state.count+1 }
        })
    }
}
